#include "StdAfx.h"
#include "ParametricUnitTest.h"
#include <algorithm>
#include <shlobj.h>
#include "ProToolkit.h"
#include "ProCore.h"


using namespace std;


#undef GetFreeSpace

#import <scrrun.dll> \
	rename("DeleteFile", "_DeleteFile"), \
	rename("MoveFile","_MoveFile"), \
	rename("CopyFile", "_CopyFile") 


using namespace Scripting;

void PrintComError(_com_error &e)
{
	// Notify the user of errors if any.
	_bstr_t bstrSource(e.Source());
	_bstr_t bstrDescription(e.Description());

	wstringstream buff;
	BSTR bstr = NULL;
	_bstr_t nilStr(bstr, false);

	buff<<L"ComError:"<<endl;
	buff<<L"Code = "<<showbase << oct<<e.Error()<<L"x"<<endl;
	buff<<L"Code10 = "<<showbase << dec<<e.Error()<<noshowbase <<endl;
	buff<<L"Code meaning = "<<e.ErrorMessage()<<endl;
	if(bstrSource != nilStr)
		buff<<L"Source = "<<(TCHAR*)bstrSource<<endl;
	if(bstrDescription != nilStr)
		buff<<L"Description = "<<(TCHAR*)bstrDescription<<endl;

	wcout<<buff.str();

}

UnitTestZxs::UnitTestZxs(void)
{
}

UnitTestZxs::~UnitTestZxs(void)
{
}

void UnitTestZxs::SetUp()
{
	
	_bstr_t dir = typeid(*this).name();
	outputdir = (LPCTSTR)dir;
	
	outputdir.replace(0,6,L"");

	size_t i = outputdir.find(L"_Test");
	if(i != wstring::npos)
	{
		outputdir.replace(i,5,L"");
	}

	

	outputdir = TestResultdir + L"\\" + outputdir;

	IFileSystem3Ptr fso;
	HRESULT hr = fso.CreateInstance(__uuidof(FileSystemObject));
	assert(SUCCEEDED(hr));

	fso->CreateFolder(outputdir.c_str());
	
	SetCurrentDirectory(outputdir.c_str());

}

void UnitTestZxs::TearDown()
{

}

void UnitTestZxs::init()
{

	IFileSystem3Ptr fso;
	HRESULT hr = fso.CreateInstance(__uuidof(FileSystemObject));
	assert(SUCCEEDED(hr));

	rootdir = (LPCTSTR)fso->GetAbsolutePathName(L".");

	wchar_t tmpbuf[128];
	_tzset();

	//get system-style date and time. 
	_wstrdate_s( tmpbuf, 128 );

	wstring timestr(tmpbuf);
	std::replace( timestr.begin(), timestr.end(), '/', '_'); // replace all '/' to '_'

	_wstrtime_s( tmpbuf, 128 );
	timestr += L"_";
	timestr += tmpbuf;
	std::replace( timestr.begin(), timestr.end(), ':', '_'); // replace all ':' to '_'

	TestResultdir = rootdir + L"\\TestResult\\" + timestr;

	::SHCreateDirectoryEx(NULL,TestResultdir.c_str(),NULL); 

	SetCurrentDirectory(TestResultdir.c_str());
}

void UnitTestZxs::DeploymentItem( const wchar_t* file )
{
	wstring filepath = rootdir + L"\\" + file;
	IFileSystem3Ptr fso;
	HRESULT hr = fso.CreateInstance(__uuidof(FileSystemObject));
	assert(SUCCEEDED(hr));

	IFilePtr fileObj = fso->GetFile(filepath.c_str());

	wstring filepathTag = outputdir + L"\\" + (LPCTSTR)fileObj->Name;

	try
	{
		fso->_CopyFile(filepath.c_str(),filepathTag.c_str(),VARIANT_TRUE);
	}
	catch (_com_error& e)
	{
		PrintComError(e);
	}

}

void UnitTestZxs::DeploymentDir(const wchar_t* dir)
{
	wstring filepath = rootdir + L"\\" + dir;
	IFileSystem3Ptr fso;
	HRESULT hr = fso.CreateInstance(__uuidof(FileSystemObject));
	assert(SUCCEEDED(hr));

	IFolderPtr folderObj = fso->GetFolder(filepath.c_str());

	wstring filepathTag = outputdir + L"\\" + (LPCTSTR)folderObj->Name;

	try
	{
		fso->CopyFolder(filepath.c_str(),filepathTag.c_str(),VARIANT_TRUE);
	}
	catch (_com_error& e)
	{
		PrintComError(e);
	}
}

wstring UnitTestZxs::TestResultdir;

wstring UnitTestZxs::rootdir;


std::string proePath;
std::string textPath;

void ParametricUnitTest::SetUp()
{
	__super::SetUp();
	ProProcessHandle ProEhandle;
	ProError err;
	err= ProEngineerConnectionStart((char *)proePath.c_str(),(char *)textPath.c_str(),&ProEhandle);
	ASSERT_EQ(0,err);

}

void ParametricUnitTest::TearDown()
{
	
	 ProEngineerEnd();
	 Sleep(500);
	 __super::TearDown();
}

void ParametricUnitTest::PRO_COMM_MSG_EXE( LPCTSTR path )
{
	SetEnvironmentVariable(L"PRO_COMM_MSG_EXE",path);
}

void ParametricUnitTest::ProePath( LPCTSTR path )
{
	_bstr_t dir = path;
	proePath = (LPSTR)dir;
}

void ParametricUnitTest::TextPath( LPCTSTR path )
{
	_bstr_t dir = path;
	textPath = (LPSTR)dir;
}


